//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Research.ScientificWorkflow.TridentModel;
using System.IO;
using System.Xml;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.Research.DataLayer;
using System.CodeDom.Compiler;

namespace Microsoft.Research.ScientificWorkflow.TridentCompiler
{
    public class TridentWorkflowCompiler
    {
        string tempFileName = "temp.xoml";

        string completeTempXomlPath;

        /// <summary>
        /// This function Validates the FIle for compatibility with Workflow.
        /// </summary>
        /// <param name="workflowFileName"></param>
        /// <returns></returns>
        public ValidationErrorCollection ValidateWorkflow(string workflowFileName, string workflowDllDirectoryName)
        {
            // Compile the workflow          
            WorkflowCompiler compiler = new WorkflowCompiler();
            WorkflowCompilerParameters parameters = new WorkflowCompilerParameters();
            WorkflowCompilerResults compilerResults;
            ValidationErrorCollection errors = new ValidationErrorCollection();

            try
            {
                this.SaveXOMLToTemp(workflowFileName);

                //Add the DLL reference
                string[] Dlls = Directory.GetFiles(workflowDllDirectoryName, "*.dll");
                foreach (string dllFileName in Dlls)
                {
                    if (File.Exists(dllFileName))
                    {
                        parameters.ReferencedAssemblies.Add(dllFileName);
                    }
                }

                //Add the exe reference
                string[] exeS = Directory.GetFiles(workflowDllDirectoryName, "*.exe");
                foreach (string dllFileName in exeS)
                {
                    if (File.Exists(dllFileName))
                    {
                        parameters.ReferencedAssemblies.Add(dllFileName);
                    }
                }

                compilerResults = compiler.Compile(parameters, this.completeTempXomlPath);
            }
            finally
            {
                if (File.Exists(this.completeTempXomlPath))
                {
                    File.Delete(this.completeTempXomlPath);
                }
            }

            foreach (CompilerError compilerError in compilerResults.Errors)
            {
                errors.Add(new ValidationError(
                    compilerError.ErrorNumber + " : " + compilerError.ErrorText
                    , 0x15b));
            }

            return errors;
        }

        /// <summary>
        /// This method downloads Save the Temp XOML from teh Original
        /// </summary>
        private void SaveXOMLToTemp(string workflowFileName)
        {
            XmlDocument doc = new XmlDocument();

            // Download the XOML file into TempFile.
            this.completeTempXomlPath = Path.GetTempPath() + tempFileName;

            // copy to a temporary file and add the x:Class attribute        
            doc.Load(workflowFileName);
            doc.DocumentElement.SetAttribute("Class", "http://schemas.microsoft.com/winfx/2006/xaml", "test");
            doc.Save(this.completeTempXomlPath);
        }
    }
}
